<template>
	<view class="user_info pages">
		<view class="container">
			<view class="item header_img">
				<view class="title">{{$t('mine.userInfo.Avatar')}}</view>
				<view class="info" @tap="modify('headImg')">
					<view class="info_txt">
						<image :src="userInfo.img || userInfo.avatar" mode="widthFix"></image>
					</view>
					<uni-icons type="forward" color="#bbb" size="20"></uni-icons>
				</view>
			</view>
			<view class="item">
				<view class="title">{{$t('mine.userInfo.Nickname')}}</view>
				<view class="info" @tap="modify('nickname')">
					<view class="info_txt">{{userInfo.nick_name}}</view>
					<uni-icons type="forward" color="#bbb" size="20"></uni-icons>
				</view>
			</view>
			<view class="item">
				<view class="title">{{$t('mine.userInfo.Signature')}}</view>
				<view class="info" @tap="modify('sign')">
					<view class="info_txt">{{userInfo.desc||$t('mine.userInfo.Not')}}</view>
					<uni-icons type="forward" color="#bbb" size="20"></uni-icons>
				</view>
			</view>
			<view class="item">
				<view class="title">{{$t('mine.userInfo.Gender')}}</view>
				<view class="info" @tap="modify('sex')">
					<view class="info_txt">{{userInfo.sex==1?$t('mine.userInfo.Man'):userInfo.sex==2?$t('mine.userInfo.Woman'):$t('mine.userInfo.Not')}}</view>
					<uni-icons type="forward" color="#bbb" size="20"></uni-icons>
				</view>
			</view>
			<view class="item">
				<view class="title">{{$t('mine.userInfo.Birthday')}}</view>
				<view class="info" @tap="modify('birthday')">
					<view class="info_txt">{{userInfo.birthday||$t('mine.userInfo.Not')}}</view>
					<uni-icons type="forward" color="#bbb" size="20"></uni-icons>
				</view>
			</view>
		</view>
		<view class="btn" @click="userUpdateUser">{{$t('mine.userInfo.Save')}}</view>
		<uni-popup ref="issue">
			<view class="issue-content">
				<view class="issue-pos">
					<uni-file-picker limit="1" file-mediatype="all" @select="select">{{$t('mine.userInfo.Choose')}}</uni-file-picker>
				</view>
				<view class="issue-btn" @click="closeIssue">{{$t('mine.userInfo.Cancel')}}</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import { userIndex, userUpdateUser } from '@/api/api.js'
	export default {
		data() {
			return {
				userInfo: {}
			}
		},
		onLoad() {
			this.userIndex()
		},
		methods: {
			modify(key) {
				switch (key) {
					case "headImg":
						this.openIssue()
						break;
					case "nickname":
						this.goModify(key)
						break;
					case "sign":
						this.goModify(key)
						break;
					case "sex":
						this.goModify(key)
						break;
					case "birthday":
						this.goModify(key)
						break;
					default:
						break;
				}
			},
			goModify(key) {
				uni.navigateTo({
					url: "/pages/shop/mine/modifyInfo/modifyInfo?key=" + key
				})
			},
			userIndex(){
				userIndex({
					token: uni.getStorageSync('token')
				}).then(res=>{
					this.userInfo = res.data.data
				})
			},
			// 获取上传状态
			select(e) {
				this.closeIssue()
				const uploadTask = uni.uploadFile({
					url: 'https://appzdm.com/api/common/upload',
					methods: 'POST',
					formData: {
						token: uni.getStorageSync('token'),
						file: e.tempFilePaths[0]
					},
					name: 'file',
					filePath: e.tempFilePaths[0],
					success: (res) => {
						this.userInfo.image = JSON.parse(res.data).data.url
						this.userInfo.img  = JSON.parse(res.data).data.fullurl
						// console.log('上传成功');
						// 上传成功后修改用户头像信息
						this.userUpdateUser()
						this.$forceUpdate() // 页面强制刷新
					}
				})
			},
			userUpdateUser(){
				userUpdateUser({
					token: uni.getStorageSync('token'),
					avatar: this.userInfo.image,
					birthday: this.userInfo.birthday,
					sex: this.userInfo.sex,
					nick_name: this.userInfo.nick_name,
					desc: this.userInfo.desc
				}).then(res=>{
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					})
					setTimeout(() => {
						this.userIndex()
					}, 600)
				})
			},
			openIssue() {
				this.$refs.issue.open('bottom')
			},
			closeIssue() {
				this.$refs.issue.close()
			}
		},
		onShow() {
			uni.setNavigationBarTitle({
				title: this.$t('list.Bianji')
			})
		}
	}
</script>

<style lang="scss" scoped>
	.user_info {
		width: 100%;
		height: 100vh;
		background-color: #fafafa;

		.container {
			width: 100%;
			background: #fff;

			.item {
				height: 130rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin: 0 40rpx;
				border-bottom: 2rpx solid #ebebf0;

				.title {
					font-size: 32rpx;
					line-height: 32rpx;
					font-weight: 500;
				}

				.info {
					display: flex;
					align-items: center;

					.info_txt {
						margin-right: 20rpx;
						font-size: 28rpx;
						color: #999;
					}
				}
			}

			.header_img {
				height: 200rpx;

				.info {
					.info_txt {
						width: 120rpx;
						height: 120rpx;
						border-radius: 50%;
						overflow: hidden;
						background: #c9c8db;

						uni-image {
							width: 100%;
							height: 100%;
						}
					}
				}
			}
		}
	}
	.btn{
		position: absolute;
		bottom: 150px;
		left: 10%;
		right: 10%;
		background-color: #00C2C2;
		color: #fff;
		height: 50px;
		line-height: 50px;
		text-align: center;
		border-radius: 5px;
	}
	.issue-content {
		background-color: #fff;
		border-radius: 20rpx 20rpx 0 0;
		text-align: center;
		
		.issue-pos {
			height: 100rpx;
			line-height: 100rpx;
			font-size: 34rpx;
		}
	
		.issue-btn {
			height: 100rpx;
			line-height: 80rpx;
			border-top: 20rpx solid #F4F3F8;
		}
	}
</style>